package transfer

import (
	"errors"
	D "websocket_apn/define"
	wb "websocket_apn/transfer/net/websocket"
	st "websocket_apn/transfer/serial"
)

// Transfer 接口定义
type Transfer interface {
	Init()
	Send(m *D.Message) D.ErrorCode
	Receive(length int, msgId int32) (D.ErrorCode, *D.Message)
	Close()
}

//NewTransfer 构造函数
func NewTransfer(param interface{}) (Transfer, error) {
	if param == nil {
		return nil, errors.New("param is nil")
	}

	switch param.(type) {
	case wb.Param:
		np := param.(wb.Param)
		nt, err := wb.NewWebTransfer(np)
		if err != nil {
			return nil, err
		}

		return nt, nil

	case st.Param:
		np := param.(st.Param)
		nt, err := st.NewSerialTransfer(np)
		if err != nil {
			return nil, err
		}

		return nt, nil
	default:
		return nil, errors.New("unknown param type")
	}
}
